Skip to main content

Modular Arithmetic

let m be a fixed natural number greater than 1. we say b modulo m if a - b is divisible by m. ab(modm)a\equiv b\pmod m

  1. ab(modm)a\equiv b\pmod m, then ba (modm)b\equiv a\ \pmod m
  2. ab(modm)a\equiv b\pmod m, and bc (modm)b\equiv c\ \pmod m, then ac(modm)a\equiv c \pmod m
  3. ab(modm),a,b0    a=k1m+r1,b=k2m+r2,r1=r2a\equiv b\pmod m, a,b\ge0 \iff a = k_1m+r_1, b = k_2m+r_2, r_1 =r_2
  4. m,ab(modm),b{n0n<m}\forall m, a\equiv b \pmod m, b\in\{n| 0\le n < m\}]
  5. ab(modm),cd(modm)a\equiv b\pmod m, c\equiv d\pmod m
    1. (a+c)(b+d)(modm)(a + c) \equiv (b + d) \pmod m
    2. acbd(modm)ac \equiv bd \pmod m
  6. ab(modm)    anbn(modm),nNa\equiv b\pmod m\implies an\equiv bn\pmod m, n\in \mathbb{N}
  7. pPrime,pa,abac(modm)    bc(modm)p\in Prime, p \nmid a,ab\equiv ac\pmod m\implies b\equiv c\pmod m

Proof

    1. m(ab)    m(ba)    ba (modm)m\mid(a-b)\implies m\mid(b-a)\implies b\equiv a\ \pmod m
    1. ab(modm)a\equiv b\pmod m, and bc (modm)b\equiv c\ \pmod m
    2. m(ab),m(bc)m\mid(a-b), m\mid(b-c)
    3. b=ak1m=ck2mb = a - k_1m = c - k_2 m
    4. ac=(k1k2)m    maca-c = (k_1-k_2)m\implies m \mid a -c
    5. ac(modm)a\equiv c \pmod m
  1. Prove by contradiction

  2. ab(modm),a,b0a\equiv b\pmod m, a,b\ge0

  3. let a=k1m+r1,b=k2m+r2,r1r2a = k_1m+r_1, b = k_2m+r_2, r_1 \ne r_2

  4. ab=(k1k2)m+(r1r2)a-b = (k_1 - k_2)m + (r_1 - r_2)

  5. ab=k3ma - b = k_3m by 1.

  6. contradiction so a=k1m+r1,b=k2m+r2,r1=r2a = k_1m+r_1, b = k_2m+r_2, r_1 =r_2

  7. let a=k1m+r1,b=k2m+r2,r1=r2a = k_1m+r_1, b = k_2m+r_2, r_1 = r_2

  8. ab=(k1k2)m+(r1r2)=(k1k2)ma-b = (k_1 - k_2)m + (r_1 - r_2) = (k_1 - k_2)m

  9. ab(modm),a,b0a\equiv b\pmod m, a,b\ge0

  10. well, it's very obviously

    1. m(ab),m(cd)m\mid(a-b), m\mid(c-d)
    2. ab=k1m,cd=k2ma - b = k_1m, c -d = k_2 m
    3. (a+cbc)=(k1k2)m(a + c - b - c) = (k_1 - k_2)m
    4. (a+c)(b+d)(modm)(a + c) \equiv (b + d) \pmod m
    5. acbc+bc+bd=c(ab)+b(cd)=ck1m+bK2m=(ck1+bk2)mac-bc + bc +bd = c(a-b) + b(c-d) = ck_1m + b K_2 m= (ck_1 + bk_2)m
    6. acbd(modm)ac \equiv bd \pmod m

Some chinese notes following

​定义同余: ab(modm)    m(ab)    b==ka%ma\equiv b \pmod m \iff m | (a-b) \iff b == ka\%m

我们可以得出以下结论。

  • ab(modn)ab(modm)    ab(modlcm(m,n))a\equiv b \pmod n \land a\equiv b \pmod m \implies a\equiv b \pmod {lcm(m,n)}
    • mabnab    mnabm|a -b\land n|a-b \implies mn | a-b
  • gcd(k,m)=dka=ka(modm)    a=a(modmd)gcd(k,m)=d \land ka = ka' \pmod m\implies a = a' \pmod {\frac{m}{d}}
    • mk(aa)    mdkd(aa)m|k(a-a')\implies \frac{m}{d}| \frac{k}{d} (a-a')
    • since gcd(k,m)=d    mdkd(aa)=md(aa)gcd(k,m) = d \implies \frac{m}{d}| \frac{k}{d} (a-a') = \frac{m}{d}| (a-a')
  • ab(modm)bc(modm)    ac(modm)a \equiv b\pmod m \land b \equiv c\pmod m \implies a \equiv c \pmod m
    • m(ab)    km+b=am|(a-b) \implies km+b = a
    • m(bc)    jm+c=bm|(b-c) \implies jm + c = b
    • km+jm+c=a    m(ac)km+jm+c = a \implies m|(a-c)

定义φ(m)\varphi(m)<m< m的数中与m互素的个数(i.e. φ(12)=4\varphi(12) = 4 其中1,5,7,111,5,7,11),经过归纳总结我们得到φ(m)=mpm(11p)\varphi(m) = m\prod_{p|m}(1-\frac{1}{p}),这个function被称为欧拉函数。 若 mm 质数啧φ(m)=m1\varphi(m) = m - 1

  • 例如 φ(30)=8=3030/230/330/5+30/2/3+30/2/5+30/3/530/2/3/5\varphi(30) = 8=30 - 30/2 - 30/3 - 30/5 + 30/2/3 + 30/2/5 + 30/3/5 - 30/2/3/5 减去各个倍数并加上重复减去的
  • 而这个式子为30(11/2)(11/3)(11/5)30(1-1/2)(1-1/3)(1-1/5)

利用欧拉函数的性质我们可以得到gcd(a,m=1    aφ(m)1(modm)gcd(a,m = 1\implies a^{\varphi(m)}\equiv 1 \pmod m这个性质被称为欧拉定理

  • 例子: φ(6)=2\varphi(6)=2 let a=7a = 7 where gcd(7,6)=1gcd(7, 6) = 1
  • 721=48/6=8    761(modm)7^2-1 = 48/6 = 8 \implies 7^6 \equiv 1 \pmod m
  • 具体: for φ(m)\varphi(m) there are φ(m)\varphi(m) numbers relative prime to mm denote them as r1,,rφ(m)r_1, \ldots, r_{\varphi(m)}. Then for any aN,gcd(a,m=1a \in \N, gcd(a,m = 1, a×ri,1iφ(m)a\times r_i, 1 \le i \le \varphi(m) there exists a rj,1jφ(m),s.t,a×rirj(modm)r_j, 1 \le j \le \varphi(m), s.t, a\times r_i \equiv r_j \pmod m. And also no such repeat for those numbers so that we may have ar1××arφ(m)r1××rφ(m)(modm)ar_1 \times \ldots\times ar_{\varphi(m)} \equiv r_1\times \ldots \times r_{\varphi(m)} \pmod m where since gcd(ri,m=1gcd(r_i, m = 1 then aφ(m)1(modm)a^{\varphi(m)} \equiv 1 \pmod m

在标准数论背景下的逆元我们定义为gcd(a,m=1    axmy=1gcd(a,m=1\implies ax - my =1 or ax1(modm)ax\equiv 1 \pmod m such xx 被我们称之为aa的逆元 一般情况下denote a1a^{-1}

  • a/b(modm)a/b \pmod m 我们可将其视为 ab1(modm)ab^{-1} \pmod m
  • 最快求出逆元的方式我们用到欧拉定理(其实是费马小定理这里) ap11(modp))    aap21(modp))    a^{p-1}\equiv 1 \pmod p) \implies a* a^{p-2}\equiv 1 \pmod p)\implies ap2a1(modp) a^{p-2} \equiv a^{-1} \pmod p 换一下就是φ(m)=p\varphi(m) = p
  • 常用模数 1e9+7,9982443531e9 + 7,998244353
  • 对于 aaaa关于 pp的逆元我们也可以用公式 a1=((pp/a)(p(moda))1)(modp)a^{-1} = ((p - p/a)*(p\pmod a)^{-1}) \pmod p
    • 首先我们可以得到 p=p(moda)+p/aap = p\pmod a + \lfloor p/a\rfloor * a
    • 同样的我们可以得到 0(p(moda)+p/aa)(modp)0\equiv (p\pmod a + \lfloor p/a\rfloor * a )\pmod p
    • 紧接着上面式子我们可以得到p/aa(p(moda))(modp)    p/a(p(moda))1a1(modp)-\lfloor p/a\rfloor * a\equiv (p \pmod a )\pmod p \implies -\lfloor p/a\rfloor *(p \pmod a )^{-1} \equiv a^{-1} \pmod p
    • a1=pp/a(p(moda))1(modp)a^{-1} = p-\lfloor p/a\rfloor *(p \pmod a )^{-1} \pmod p 根据同余性质 a1=pp/a(p(moda))1(modp)    a1=((p(moda))1)pp/a(p(moda))1(modp)a^{-1} = p-\lfloor p/a\rfloor *(p \pmod a )^{-1} \pmod p \iff \\a^{-1} = ((p \pmod a )^{-1})p-\lfloor p/a\rfloor *(p \pmod a )^{-1} \pmod p
    • 最终得到a1=((pp/a)(p(moda))1)(modp)a^{-1} = ((p - p/a)*(p\pmod a)^{-1}) \pmod p

对于任意整数数列aia_i, 我们可以先得到他们的乘积S0=1,S1=a1,S2=S1a2,Si=Si1aiS_0 = 1, S_1 = a_1, \\ S_2 = S_1*a_2, S_i = S_{i-1}*a_i. 然后直接对SiS_i进行求解逆元得到Ti=(Si)1T_i = (S_i)^{-1}我们发现Ti1=(Si)1aiT_{i-1} = (S_i)^{-1}*a_i 最终得到T1=(S1)1=(a1)1T_1 = (S_1)^{-1} = (a_1)^{-1} 同样的,对上述值进行复原a21=T2S1,a_2^{-1} = T_2 *S_1, a31=T3S2a_3^{-1} = T_3 *S_2 and so on.

Chinese Remainder Theorem(CRT)

Chinese Remainder Theorem:给定方程组xai(modmi)x \equiv a_i \pmod {m_i}, 如果 k,l[0,i],kl,gcd(mk,ml)=1    x[0,(imi)1]\forall k,l \in [0, i], k\ne l ,\gcd(m_k, m_l) =1 \implies x \in [0, (\prod_{i} {m_i}) - 1] is unique

解 such 方程组我们可以构造方程从而获得解: loop i: 设ai=1,x0(modm)j,ji,x1(modmi)a_i = 1, x\equiv 0 \pmod m_j, j\ne i, x \equiv 1\pmod {m_i}解这种方程得到特定解sis_i. 在求得所有特定解后我们可以的到 x=(imisi)(modimi)x = (\sum_{i}{m_i}s_i)\pmod {\prod_{i} {m_i}}

但是mi{m_i} 互质的情况过于稀少,一般的我们会解xai(modmi),xai+1(modmi+1)x\equiv a_i\pmod {m_i}, x\equiv a_{i+1}\pmod {m_{i+1}} 得到应该新的解并重复该操作直到得到最后正确解

例子 xa(modb),xc(modd)x\equiv a \pmod b, x\equiv c\pmod d通过第一个我们得到x=bt+ax = bt + a 并将其带入第二个方程中得到bt+ac(modd))bt + a \equiv c \pmod d)bt+a=dk+cbt+a = dk+c 得到tt的解 ``

并利用 b/g t(ca)/g(modd/g),g=gcd(b,d)b/g\ t \equiv (c-a)/g \pmod {d/g}, g = gcd(b, d) 得到 tt(ca)/g(modd/g)t \equiv t*(c-a)/g \pmod {d/g} .

进行转化btbt(ca)/g(modbd/g)    bt+abt(ca)/g+a(modbd/g)bt \equiv bt*(c-a)/g \pmod {bd/g} \implies bt + a\equiv bt*(c-a)/g + a \pmod {bd/g}, 最终得到 xbt(ca)/g+a(modbd/g)x \equiv bt*(c-a)/g + a \pmod {bd/g}


对于一组方程组xai(modmi)x \equiv a_i \pmod {m_i}, 我们可以对合数mi{m_i} 根据中国剩余定理拆成 piej\prod p_i^{e_j}然后把所有差分解搜集出来做对比,如果不对的话说明无解

例: 给一个方程组x12(mod36),x1(mod4)x\equiv 12 \pmod {36}, x \equiv 1\pmod 4

第一个方程我们可以拆解为x0(mod4),x3(mod9)x \equiv 0 \pmod 4, x\equiv 3\pmod 9 很显然有冲突所以无解。